var curpage=1;
$(function() {
    var userInfo = getLocalUserInfo();
    checkUserLogin();
    var myDate = new Date((new Date()).getTime() - 86400 * 30 * 1000);
    var sdate = $('.sdate');
    var edate = $('.edate');
//  sdate.val((myDate).Format('yyyy-MM-dd'));
    sdate.val((new Date()).Format('yyyy-MM-dd'));
    console.log(userInfo.Pid);
   
    // 时间
    var dtPicker = new mui.DtPicker({type:"date"});	
    // 选择开始时间
	$('.sdate').parent().on('tap', function () {
	    dtPicker.show(function (item) {
		    $('.sdate').val(item.value);
	    });
	});
		
	// 选择截止时间
//	$('.edate').parent().on('tap', function () {
//	    dtPicker.show(function (item) {
//		    $('.edate').val(item.value);
//	    });
//	});
	//仓库
	var picker = new mui.PopPicker(); 
	function depot(){
		$.ajax({
            type: "post",
            url: ApiUrl,
            ontentType: 'application/json',
            data:JSON.stringify({
              	"op": "Store",
            }),
            dataType: "json",
            success: function(e) {
            	console.log(e.Main);
            	depot_data= e.Main.map(function(item){
            		var obj = Object.values(item)
            		return{
            			value:obj[1],
            			text:obj[0]
            		}
            	})
            }
        })
	}
	$('#depot').on('tap', function () {
		depot();
		picker.setData(depot_data);
		picker.show(function (item) {
			$('#depot').html(item[0].text).attr('data-val',item['0'].value);
		});
	});
	//类型
//	$('#search-type').on('tap', function () {
//		picker.setData([
//			{value:'',text:'全部'},
//			{value:'正常入库单',text:'正常入库单'},
//			{value:'物权转移',text:'物权转移'},
//			]);
//		picker.show(function (item) {
//			$('#search-type').html(item[0].text).attr('data-val',item['0'].value);
//		});
//	});

	//入库方式
	$('#rktype').on('tap', function () {
		picker.setData([
			{value:'全部',text:'全部'},
			{value:'普通',text:'普通'},
			{value:'保税',text:'保税'},
			]);
		picker.show(function (item) {
			$('#rktype').html(item[0].text).attr('data-val',item['0'].value);
		});
	});
	
    //列表
    function list() {
    	if(curpage === 1) {
	    // 初始化加载
		    var html = '<div class="sctouch-norecord search">\n' +
		        '        <div class="norecord-ico"><i class="iconfont"></i></div>\n' +
		        '        <dl>\n' +
		        '            <dt>数据加载中...</dt>\n' +
		        '        </dl>\n' +
		        '    </div>';
		    $('.list').html(html);
	    }
    	sdate = $('.sdate').val();
    	//edate = $('.edate').val();
    	var key_word = $('#order_key').val();
    	var key_word2 = $('#order_key2').val();
    	var depot = $('#depot').data('val') || '';
    	var qdcode = $('#qdcode').data('val');
    	var rktype = $('#rktype').data('val') || '全部';
    	console.log(depot);
    	//var search_type = $('#search-type').data('val') || '';
        $.ajax({
            type: "post",
            url: ApiUrl,
            ontentType: 'application/json',
            data:JSON.stringify({
              	"op": "KCDetail",
              	"keyword": key_word,
				"Pid": userInfo.Pid,
				"sdate": sdate,
				//"edate": edate,
				"unitname":key_word2,
				"sname":depot,
				"qdcode":qdcode,
//				"typename":search_type
	            "rktype":rktype
            }),
            dataType: "json",
            success: function(e) {
            	curpage++;
            	console.log(e);
            	var html = template('order-list-tmpl', e);
                $('.list').html(html);
            }
        })
    }
    list();
    
	// 点击查询
    $('#header-nav').on('tap', function() {
       list();
    });

    // 页面数据重新加载
    window.pageReload = function () {
        // 重新加载列表
        reset = true;
        list();
    }
});
